using System.Collections;
using UnityEngine;
using System;
using System.Threading;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.IO;
using System.Text;
using IMIForUnity;


namespace IMIForUnity
{	
	public  partial class BodyMeaMgrImp
	{

		float chestRatio = 1f; //0.88f;      //  0.98f;
		public float chestWidth = 0;
		public float chestThickness = 0f;

		public float chestWidthtmp = 0f;

		public ArrayList chestSmthWidthArr=new ArrayList();
		public ArrayList chestSmthThicknessArr=new ArrayList();
		


		void ProcessChestSide(ref Dictionary<int, ImiPlayerInfo> playerInfos , int userID, bool isDrawOrNot)
		{
			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;




			int NeckPixPosX=0;
			int NeckPixPosY=0;
			int NeckPixPosZ=0;
			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref NeckPixPosX, ref NeckPixPosY, ref NeckPixPosZ, 
			                      ImiSkeleton.Index.SHOULDER_CENTER,false);

			int shoulderMidPixPosX=0;
			int shoulderMidPixPosY=0;
			int shoulderMidPixPosZ=0;

			GetPlayerJointPixPos (ref playerInfos ,userID, ref shoulderMidPixPosX, ref shoulderMidPixPosY, ref shoulderMidPixPosZ, 
			                     ImiSkeleton.Index.SHOULDER_CENTER,false);

			int spineMidPixPosX=0;
			int spineMidPixPosY=0;
			int spineMidPixPosZ=0;
						
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref spineMidPixPosX, ref spineMidPixPosY, ref spineMidPixPosZ, 
			                     ImiSkeleton.Index.SPINE,false);

			chestPixPosX=(shoulderMidPixPosX+spineMidPixPosX)/2;
			chestPixPosY=(shoulderMidPixPosY+spineMidPixPosY)/2;
			chestPixPosZ=(shoulderMidPixPosZ+spineMidPixPosZ)/2;

		

///method after 11 22. Use an range between shoulder center and spine.

			int chestsideupx=RangeValue(chestPixPosX, 0 ,dw);
			//int chestsideupy=RangeValue(chestPixPosY+(int)(((float)(shoulderMidPixPosY-chestPixPosY))/3)*2, 0 ,dh);
			int chestsideupy=RangeValue(chestdownY+(int)(((float)(NeckPixPosY-chestdownY))/3), 0 ,dh);
			int chestsidedownx=RangeValue(chestPixPosX, 0 ,dw);;
			//int chestsidedowny=RangeValue(chestPixPosY+(int)(((float)(spineMidPixPosY-chestPixPosY))/4)*3, 0 ,dh); 
			int chestsidedowny=RangeValue(chestdownY+(int)(((float)(chestdownY-NeckPixPosY))/3), 0 ,dh); 




			Vector2 ChestUpPixPos = new Vector2(chestsideupx,chestsideupy );
			Vector2 ChestDownPixPos = new Vector2(chestsidedownx,chestsidedowny );
			
			
			//we add a adjusting deeping value on the waist surface depth.
			
			//method 1 get the thicknest part of the hips side	by using 2 pixposes
			int tempPlayerIndex0 = depthPlayerDataOriginal  [chestsideupy *dw +chestsideupx] & 7;
			int tempPlayerIndex1 = depthPlayerDataOriginal  [chestsidedowny *dw +chestsidedownx] & 7;

			if((tempPlayerIndex0>0)&&(tempPlayerIndex1>0)&&(tempPlayerIndex0== tempPlayerIndex1 ))
			{
				
				float chestSideTmp=GetThickestPartBy2PixPoses(dw,dh,depthPlayerDataOriginal,
				                                             boundThreshval,ChestUpPixPos,ChestDownPixPos,isDrawOrNot);				
				//Debug.Log("+++++++++++++++++++++++++++hips side tmep: "+hipsSideTmp.ToString());
				//					hipsSmthThickness.InsertValue (hipsSideTmp);
				//					hipsThickness=hipsSmthThickness.GetFilteredValue();
				
				chestSmthThicknessArr.Add(chestSideTmp);
			}
		}

		//In 2.17 06.  For now we use following func locate the hori chest line.
		public void ProcessChest2(ref Dictionary<int, ImiPlayerInfo> playerInfos ,	int userID, bool isDrawOrNot )
		{
			int dw = DEPTHWIDTH;
			int dh = DEPTHHEIGHT;
			
			
			int shoulderMidPixPosX=0;
			int shoulderMidPixPosY=0;
			int shoulderMidPixPosZ=0;
			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref shoulderMidPixPosX, ref shoulderMidPixPosY, ref shoulderMidPixPosZ, 
			                      ImiSkeleton.Index.SHOULDER_LEFT,isDrawOrNot);
			
			int spineX=0;
			int spineY=0;
			int spineZ=0;
			
			GetPlayerJointPixPos (ref  playerInfos ,userID, ref spineX, ref spineY, ref spineZ, 
			                      ImiSkeleton.Index.SPINE,isDrawOrNot);
			
			
			//To measure the hips thickness.
			int chestupx=RangeValue(spineX, 0 ,dw);;
			//int hipssideupy=RangeValue((hipsMidPixPosY+(hipsMidPixPosY-hipleftY)*2),0 ,dh); 
			int chestupy=RangeValue(shoulderMidPixPosY,0 ,dh); 
			
			
			chestdownX=RangeValue(spineX, 0 ,dw);
			chestdownY=RangeValue(spineY-(spineY-shoulderMidPixPosY)/5, 0 ,dh);
			
			
			
			Vector2 chestUpPixPos = new Vector2(chestupx,chestupy );
			Vector2 chestDownPixPos = new Vector2(chestdownX,chestdownY );
			
			
			//we add a adjusting deeping value on the waist surface depth.
			
			//method 1 get the thicknest part of the hips side	by using 2 pixposes
			int tempPlayerIndex0 = depthPlayerDataOriginal  [chestupy *dw +chestupx] & 7;
			int tempPlayerIndex1 = depthPlayerDataOriginal  [chestdownY *dw +chestdownX] & 7;
			
			
			//Only when the key pix pos symplify an player pixel.
			if((tempPlayerIndex0>0)&&(tempPlayerIndex1>0)&&(tempPlayerIndex0== tempPlayerIndex1 ))
			{
				
				if( isDrawOrNot )
				{
					if( (MinContourLimitX>0)&&
					   (MaxContourLimitX<DEPTHWIDTH-1)&&
					   (MinContourLimitX<MaxContourLimitX)
					   )
					{
						for(int i=MinContourLimitX;i<MaxContourLimitX;i++)
						{
							DrawingSpotsOnTexture.DrawPixelsSquareOnByteArray(DEPTHWIDTH,DEPTHHEIGHT,
							                                                  i,chestdownY,
							                                                  3,allUsersDepthData,0,255,0,255);
						}
						
					}
				}
			}


			//Use frontmeaMgr to test the chest condition.
			chestFrontDepthLineMgr.ClearGraph ();
			chestFrontDepthLineMgr.ClearSampleDatas ();
			chestFrontDepthLineMgr.enableDrawing = true;
			chestFrontDepthLineMgr.FillRowNormaliezedDepthDataAndFillZeroGap (DepthPlayerDataOriginal, MinContourLimitX,
			                                                                  MaxContourLimitX, chestdownY, DEPTHWIDTH, DEPTHHEIGHT);
//			chestFrontDepthLineMgr.DrawDepthAnalysisArr ();
//			chestFrontDepthLineMgr.DrawGraph ();
			LeftChestLimit = MinContourLimitX + chestFrontDepthLineMgr.sharpBeginIdx + chestFrontDepthLineMgr.armBodyGapLeftIdx;
			RightChestLimit = MinContourLimitX + chestFrontDepthLineMgr.sharpBeginIdx + chestFrontDepthLineMgr.armBodyGapRightIdx;
			
		}
		
		
		
		public float GetChestGirth2()
		{
			return   IMIBodyMeaTool.GetGirth2(chestFrontDepthLineMgr.frontLen0, chestFrontDepthLineMgr.frontLen1,
			                 chestFrontDepthLineMgr.frontSideChordThick,chestThickness,remainGirthRatioChest0,remainGirthRatioChest1);
			
		}


		public float  GetChestGirth( )
		{
			return IMIBodyMeaTool.CalculateChestGirth(chestWidth, chestThickness,chestRatio);
		}


		public float ChestfrontArcLength()
		{
			return chestFrontDepthLineMgr.frontLen0;
		}

		public float ChestfrontChordLength()
		{
			return chestFrontDepthLineMgr.frontLen1;
		}



	}
}
